//
//  AddCarVinsVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/9/12.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "AddCarVinsVC.h"
#import "AddCarVinCell.h"
@interface AddCarVinsVC ()
{
    UIImageView * nav;
    UITableView * Table;

}
@end

@implementation AddCarVinsVC
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.carListArr = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    nav = [self createNav:@"添加商品车车架号"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;

    

    
    

    NSLog(@"%@",self.carListArr);
    
    
    
    int sum = 0;
    
    if (self.carListArr.count > 0) {
        
        
        NSMutableArray * arr = self.carListArr.mutableCopy;
        
        for (int i = 0; i < arr.count; i++) {
            
            NSString * amt = [NSString stringWithFormat:@"%@",arr[i][@"amt"]];
            
            sum = sum + [amt intValue];
        }
        
        
        NSString * su = [NSString stringWithFormat:@"%lu",(unsigned long)self.carListArr.count];
        sum = sum + [su intValue];
        
        
        for (int i = 0; i<arr.count; i++) {
            
            
            NSString * amt = [NSString stringWithFormat:@"%@",arr[i][@"amt"]];

            int s = [amt intValue];

            NSMutableArray * vinList = [[NSMutableArray alloc]init];
            
            for (int j = 0; j< s; j++) {
                
                [vinList addObject:@""];
                
            }
            
            
            [self.carListArr[i] setObject:vinList forKey:@"vinList"];
            
            
        }
        
        
        
 
    }
    
    Table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 43*kHeight) style:UITableViewStylePlain];
    [Table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//    Table.scrollEnabled =NO;
    
    //UIScrollViewKeyboardDismissModeNone,//默认第一种，为none UIScrollViewKeyboardDismissModeOnDrag,//键盘会当tableView上下滚动的时候自动收起 UIScrollViewKeyboardDismissModeInteractive, // 设置键盘的消失方式为拖拉并点击页面，iOS7新增特性
    

    Table.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    Table.delegate = self;
    Table.dataSource = self;
    [self.view addSubview:Table];
    [Table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(nav.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, sum * 43*kHeight));
    }];
    
    
    UIButton * nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"确定" forState:UIControlStateNormal];
    [nextBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(pressNextBtn) forControlEvents:UIControlEventTouchUpInside];
    LRViewBorderRadius(nextBtn, 5, 0.5, YellowColor);
    nextBtn.backgroundColor = YellowColor;
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(355*kWidth, 50*kHeight));
        make.top.mas_equalTo(Table.mas_bottom).with.offset(37*kHeight);
    }];

    


}



-(void)pressNextBtn{
    
    
    if (_callBackVin) {
        
        _callBackVin(self.carListArr);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *str=@"str";
    AddCarVinCell * cell =  [tableView dequeueReusableCellWithIdentifier:str];
    if (cell ==nil) {
        cell = [[AddCarVinCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    //        NSMutableArray * vin = _CarName[indexPath.section][@"vins"];
    //        cell.field.text = vin[indexPath.row][@"vin"];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.field addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    cell.field.delegate = self;
//    [_fieldArr addObject:cell.field];
    return cell;
}

-(void)textFieldWithText:(UITextField *)textField{
    
    //00 10 20 30
    //01 11 21 31
    UITableViewCell *cell  = (UITableViewCell *)[[textField superview] superview];
    NSIndexPath *indexPath = [Table indexPathForCell:cell];
    NSLog(@"%ld%ld",(long)indexPath.row,(long)indexPath.section);
    
    
    [self.carListArr[indexPath.section][@"vinList"] replaceObjectAtIndex:indexPath.row withObject:textField.text];
    
   
    
    
    //500 501 502 503
//    NSString * string = [NSString stringWithFormat:@"%@",textField.text];
//    NSInteger index = textField.tag;
    
    //    NSMutableArray * arr = _dataDic[@"vins"];
    //
    //    NSMutableArray * arrVin = [[NSMutableArray alloc]init];
    //    for (int i=0; i<arr.count; i++) {
    //
    //        [arrVin addObject:arr[i][@"vin"]];
    //    }
    //    if ([arrVin containsObject:textField.text]) {
    //
    //        [self createUIAlertController:@"vin重复输入" andTextIndex:index];
    //    }else{
    //    }
//    if ([self textField:textField shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""]) {
//        [_dataDic[@"vins"][index] setValue:string forKey:@"vin"];
//    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 43*kHeight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43*kHeight;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    int carNumber = [self.carListArr[section][@"amt"] intValue];
    return carNumber;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return  self.carListArr.count;
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = WhiteColor;
    view.frame = CGRectMake(0, 0, Main_Width, 43*kHeight);
    
    UILabel * label = [UILabel createUIlabel:self.carListArr[section][@"brandName"] andFont:FontOfSize13 andColor:BtnTitleColor];
    [view addSubview:label];
    
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left).with.offset(20*kWidth);
        make.centerY.mas_equalTo(view.mas_centerY);
    }];

    
    UIView * lineView1= [[UIView alloc]init];
    lineView1.backgroundColor = LineColor;
    [view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left);
        make.right.mas_equalTo(view.mas_right);
        make.bottom.mas_equalTo(view.mas_bottom);
        make.height.mas_equalTo(1*kHeight);
    }];
    UIView * lineView2= [[UIView alloc]init];
    lineView2.backgroundColor = LineColor;
    [view addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view.mas_left);
        make.right.mas_equalTo(view.mas_right);
        make.top.mas_equalTo(view.mas_top);
        make.height.mas_equalTo(1*kHeight);
    }];
    
    return view;
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
